package com.wlc.controller;

import com.wlc.pojo.Employee;
import com.wlc.pojo.PageResult;
import com.wlc.pojo.Result;
import com.wlc.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 分页查询员工列表
     */
    @GetMapping("/list")
    public Result<PageResult<Employee>> list(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long deptId,
            @RequestParam(required = false) Integer workState,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("分页查询员工列表: name={}, deptId={}, workState={}, page={}, size={}",
                name, deptId, workState, page, size);
        try {
            PageResult<Employee> pageResult = employeeService.findEmployeeList(name, deptId, workState, page, size);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("查询员工列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据ID查询员工
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id) {
        log.info("根据ID查询员工: {}", id);
        try {
            Employee employee = employeeService.findById(id);
            return Result.success(employee);
        } catch (Exception e) {
            log.error("查询员工失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询所有员工
     */
    @GetMapping("/all")
    public Result<List<Employee>> getAll() {
        log.info("查询所有员工");
        try {
            List<Employee> employees = employeeService.findAll();
            return Result.success(employees);
        } catch (Exception e) {
            log.error("查询所有员工失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新增员工
     */
    @PostMapping
    public Result<String> save(@RequestBody Employee employee) {
        log.info("新增员工: {}", employee.getName());
        try {
            employeeService.save(employee);
            return Result.success("新增员工成功");
        } catch (Exception e) {
            log.error("新增员工失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新员工
     */
    @PutMapping
    public Result<String> update(@RequestBody Employee employee) {
        log.info("更新员工: {}", employee.getId());
        try {
            employeeService.update(employee);
            return Result.success("更新员工成功");
        } catch (Exception e) {
            log.error("更新员工失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除员工
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除员工: {}", id);
        try {
            employeeService.deleteById(id);
            return Result.success("删除员工成功");
        } catch (Exception e) {
            log.error("删除员工失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新员工状态
     */
    @PutMapping("/status/{id}")
    public Result<String> updateWorkState(@PathVariable Long id, @RequestParam Integer workState) {
        log.info("更新员工状态: id={}, workState={}", id, workState);
        try {
            employeeService.updateWorkState(id, workState);
            return Result.success("更新员工状态成功");
        } catch (Exception e) {
            log.error("更新员工状态失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}